package com.sky.composite;


import java.util.ArrayList;

/**
 * 抽象组件
 */
interface AbstractFile {
    void killVirus();
}


/**
 * 容器组件
 */

class Folder implements AbstractFile {
    private String name;
    private ArrayList<AbstractFile> files = new ArrayList<>();

    Folder(String name) {
        this.name = name;
    }

    void add(AbstractFile abstractFile) {
        files.add(abstractFile);
    }

    void remove(AbstractFile abstractFile) {
        files.remove(abstractFile);
    }

    AbstractFile getChild(int index) {
        AbstractFile abstractFile = files.get(index);
        return abstractFile;
    }

    @Override
    public void killVirus() {

        System.out.println(this.name + " 查杀中.....");
        for (AbstractFile abstractFile : files) {
            abstractFile.killVirus();
        }


    }


}


/**
 * 叶子节点
 */
class ImageFile implements AbstractFile {

    private String name;
    public String getName() {
        return name;
    }
    ImageFile(String name) {
        super();
        this.name = name;
    }

    @Override
    public void killVirus() {
        System.out.println(this.name + " 查杀中.....");
    }
}


/**
 * 叶子节点
 */
class VideoFile implements AbstractFile {

    String name;

    public String getName() {
        return name;
    }

    public VideoFile(String name) {
        super();
        this.name = name;
    }

    @Override
    public void killVirus() {

        System.out.println(this.name + " 查杀中.....");
    }
}


/**
 * 叶子节点
 */
class TextFile implements AbstractFile {

    String name;

    public String getName() {
        return name;
    }

    public TextFile(String name) {
        super();
        this.name = name;
    }

    @Override
    public void killVirus() {

        System.out.println(this.name + " 查杀中.....");
    }
}
/*----------------------组合模式实例: 模拟杀毒过程----------------------------------------*/

/**
 * 火绒杀毒
 * date : 2020-08-10
 *
 * @author : wushikai
 */

public class HuoRongApplication {

    public static void main(String[] args) {

        AbstractFile womanPic, loveAvi, teacherTxt;
        Folder thunderDownLoad = new Folder("迅雷下载");
        Folder videoFolder = new Folder("视频");
        womanPic = new ImageFile("美女图片.jpg");
        loveAvi = new VideoFile("苍老师.avi");
        videoFolder.add(loveAvi);
        teacherTxt = new TextFile("我和老师的那些事.txt");
        thunderDownLoad.add(videoFolder);
        thunderDownLoad.add(womanPic);
        thunderDownLoad.add(teacherTxt);
        thunderDownLoad.killVirus();
    }

}




